package com.example.demo.utils;

import com.example.demo.Controller.index;
import com.example.demo.entiy.Bgd;
import com.example.demo.entiy.XM;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.log4j.Logger;
import org.springframework.ui.Model;

import javax.servlet.http.HttpSession;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

public class MyUtils {
    //HttpSession session;
    private final Logger log=Logger.getLogger(this.getClass());
    public Boolean isExits(String parm, List<XM> li){
        boolean b=false;
        int size=li.size()-1;
        for (int i=0;i<=size;i++) {
            String xmlb = li.get(i).getXmlb();
            if (xmlb != null) {
                if (xmlb.equals(parm)) {
                    b = false;
                    return b;
                } else {
                    b = true;
                }
            } else {
                b = true;
            }
        }
        return b;
    }

    public boolean isTrueUrl(HttpSession session){
        //String status=session.getAttribute("username").toString();
       // status=status.substring(status.indexOf("-"),status.length()-1);
        try {
            if (session.getAttribute("username")==null || session.getAttribute("username")==""){
                return false;
            }else {
                return true;
            }
        }catch (Exception e){
            return  false;
        }
    }
    public String renturnUrl(String url, Model model){
        HttpSession session= index.session;
        if (session!=null) {
            boolean b = this.isTrueUrl(session);
            String username="";
            if (b) {
                username=session.getAttribute("username").toString();
                username=username.substring(0,username.indexOf("-"));
                model.addAttribute("username",username);
                return url;
            } else {
                return "/tj/index";
            }
        }else {
            return "/tj/index";
        }
    }

    public String ReturnUsername(HttpSession session){
        String status= session.getAttribute("username").toString();
        return status.substring(0,status.indexOf("-"));
    }

    public List<Bgd> RsToList(ResultSet rs) throws SQLException{
        List<Bgd> li=new LinkedList<>();

             while (rs.next()){
                Bgd bgd=new Bgd();
                bgd.setIdx(rs.getString("idx"));
                bgd.setTjbm(rs.getString("bhkcod"));
                bgd.setName(rs.getString("empnam"));
                bgd.setBirth( rs.getTimestamp("brth").toString().substring(0,10));
                bgd.setIdc(rs.getString("idc"));
                bgd.setWrknum(rs.getString("wrknum"));
                bgd.setDpt(rs.getString("dpt"));
                bgd.setSex(rs.getString("sex"));
                bgd.setAge(rs.getInt("age"));
                bgd.setWrksta(rs.getString("wrksta"));
                if(rs.getString("bhkdat")!=null && rs.getString("bhkdat")!="") {
                    bgd.setBhkdat(rs.getString("bhkdat").substring(0,10));
                }else {
                    bgd.setBhkdat("未体检");
                }
                bgd.setTjjg(rs.getString("bhkrsl"));
                bgd.setBhkzt(rs.getString("cmptag"));
                bgd.setJy(rs.getString("dgnadv"));
                bgd.setMhkadv(rs.getString("mhkadv"));
                bgd.setBhksrtidx(rs.getString("bhksrtidx"));

                bgd.setEmpidc(rs.getString("empidx"));
                //bgd.setWrkstanam(rs.getString("wrkstanam"));
                li.add(bgd);
            }
        return li;
    }
    public List<String> returnIdx2(List<Bgd> li){
        List<String> l=new LinkedList<>();
        for (int i=0;i<li.size();i++){
            l.add(i,li.get(i).getIdx());
        }
        return l;
    }
    public List<XM> returnTureList(List<XM> li) {
        List<XM> L= new LinkedList<>();
        for (int a=0;a<li.size();a++){
            XM xm=li.get(a);
            try {
                xm.setCgzs(Double.parseDouble(xm.getRes()));
            }catch (Exception e){
                log.error(e.getMessage());
            }
            L.add(xm);
        }
            return L;
        //return super.equals(obj);
    }
}
